// pages/news/news.js
//获取应用实例
const app = getApp();
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    loadmoreText: '加载更多数据...'
  },

  onLoad() {
    wx.startPullDownRefresh();
  },

  /**
   * 监听下拉刷新
   */
  onPullDownRefresh() {
    this.setData({ page: 1 });
    this.getData('update');
  },
  /**
   * 监听上拉加载
   */
  onReachBottom() {
    this.setData({ page: this.data.page+1 });
    this.getData('loadmore');
  },
  /**
   * 获取数据
   */
  getData(type) {
    app.request('/New/NewList', 'GET', { page: this.data.page, pageSize: 10 }, res => {
      if (type == 'update') {
        this.setData({list: [] });
        wx.stopPullDownRefresh();
      }

      res.list.map(item => {
        item.createTime = util.formatTime(item.createTime);
        return item;
      });
      var newList = this.data.list.concat(res.list);
      var loadmoreText = res.list.length < 10 ? '没有更多数据了' : '加载更多数据...';
      this.setData({ list: newList, loadmoreText: loadmoreText });
    });
  }
})